package com.concerneyes.trytoglasses;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.Ssiwo.sdk.SsiwoFacenginActivity;
import com.Ssiwo.sdk.UnityPlayerActivity;
import com.concerneyes.R;
import com.concerneyes.glasses.adapter.ViewPagerAdapter;
import com.unity3d.player.UnityPlayer;

import java.util.ArrayList;
import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;
import common.AbsBaseView;
import util.ToastHelper;
import util.Util;
import view.MyUniversalDialog;
import view.RoundImageView;


/**
 * 试戴View
 */
public class TryToGlassesView extends AbsBaseView implements AdapterView.OnItemSelectedListener, View.OnClickListener, PlatformActionListener {
    private FragmentPagerAdapter adapter;
    private Context mContext;
    private ViewPagerAdapter pagerAdapter;

    private ViewPager viewPager;
    private ImageView ivShare;
    private MyUniversalDialog dialog;
    private SsiwoFacenginActivity activity;
    private LinearLayout WearViewParent;
    private ArrayList<String> skus;
    private int a = 0;
    /**
     * QQ好友分享对象
     */
    private Platform platform_qqFriend;
    /**
     * 微信好友分享对象
     */
    private Platform platform_wxFriend;
    /**
     * 朋友圈分享对象
     */
    private Platform platform_circle;

    //添加静态变量
    public static UnityPlayer wearEngin;
    //判断是否第一次进入
    private boolean isFrist = true;
    private View wearView;
    private LinearLayout ll_try;
    private UnityPlayer mPlayer;

    public TryToGlassesView(Context context, SsiwoFacenginActivity activity, UnityPlayer mPlayer) {
        super(context);
        this.mContext = context;
        this.activity = activity;
        this.mPlayer = mPlayer;

        initVr();
        initView();
    }

    private void initVr() {
        ll_try = (LinearLayout) findViewById(R.id.ll_try);
        ll_try.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);

        WearViewParent = (LinearLayout) findViewById(R.id.WearViewParent);
        //给静态变量赋值
        wearEngin = mPlayer;
        //得到试戴View
        //将试戴View添加进父布局（只有将试戴View添加进去，试戴页面才会显示，可根据具体需求选择添加时机）
        wearView = activity.getWearView();
        WearViewParent.addView(wearView);

        //测试眼镜sku集合
        skus = new ArrayList<>();
        skus.add("SW_SFC_2101_54_c65_61_61_61_65");
        skus.add("SW_SFC_2102_50_c70_89_70_89_70");
        skus.add("SW_SFC_2102_52_c84_84_84_84_84");
        skus.add("SW_SFC_2104_52_c64_64_64_64_64");
        skus.add("SW_SFC_2109_52_c61_61_61_61_61");
        skus.add("SW_SFC_2123_50_c50_50_50_50_50");
        skus.add("SW_SFC_2125_50_c65_65_65_65_65");
        skus.add("SW_SFC_2126_50_c60_50_50_50_60");
        skus.add("SW_SFC_2127_54_c60_60_60_60_60");
        skus.add("SW_SFC_2130_52_c60_67_67_67_60");
        skus.add("SW_SFC_2500_54_c64_33_33_33_64");
        skus.add("SW_SFC_2503_48_c89_53_53_53_89");

        /**
         * 请填写本公司提供的appId,
         * appId在appId.txt里面
         */
        String appId = "8AAfkhUKpU0sjeQrDtC6SrhRKNqA6hGb";
        //进行app身份校验
        activity.checkAppID(appId);
        //试戴指定sku的眼镜,点击可切换
        activity.wearGlasses(skus.get(1));
    }

    public void onPause() {
        WearViewParent.removeView(wearView);
    }

    public void onResume() {
        if (isFrist) {
            mPlayer.resume();
            isFrist = false;
        } else {
            WearViewParent.addView(wearView);
        }
    }

    @Override
    public int layoutResId() {
        return R.layout.view_tryto_glasses;
    }

    @Override
    public void loadData(Object obj) {
    }

    @Override
    public boolean isEmpty() {
        return false;
    }

    public void initView() {
        ViewPager vp = (ViewPager) findViewById(R.id.viewPager);
        ivShare = (ImageView) findViewById(R.id.iv_share);
        ivShare.setOnClickListener(this);

        ViewGroup.LayoutParams lp = vp.getLayoutParams();
        lp.width = mContext.getResources().getDisplayMetrics().widthPixels;
        lp.height = mContext.getResources().getDisplayMetrics().heightPixels / 3;
        vp.setLayoutParams(lp);
        vp.setOffscreenPageLimit(10);
        //左右都缩进一个合适的值
        vp.setPageMargin((int) (-getResources().getDisplayMetrics().widthPixels / 3 * 2 * 0.9));
        //中间大 ,两边大小渐变的动画变化,最大1.4倍
        vp.setPageTransformer(true, new ViewPager.PageTransformer() {
            public void transformPage(View page, float position) {
                if (position < -1) {
                    // This page is way off-screen to the left.
                } else if (position <= 1) {
                    if (position < 0) {
                        if (position < -0.4f)
                            position = -0.4f;
                        page.setScaleY(1.4f + position);
                        page.setScaleX(1.4f + position);
                    } else {
                        if (position > 0.4)
                            position = 0.4f;
                        page.setScaleY(1.4f - position);
                        page.setScaleX(1.4f - position);
                    }
                } else {
                    // This page is way off-screen to the right.
                }
            }
        });
        //new6个view
        final ArrayList<AbsBaseView> list = new ArrayList<>();
        for (int i = 0; i < 6; i++) {
            list.add(new ViewPage(getContext(), "aaaaaaa"));
        }
        //设置Adapter
        pagerAdapter = new ViewPagerAdapter(mContext, list);
        vp.setAdapter(pagerAdapter);
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                String glassesInfo = skus.get(a);
                a++;
                if (a >= skus.size() - 1) {
                    a = 0;
                }
                /**
                 * 请填写本公司提供的appId,
                 * appId在appId.txt里面
                 */
                String appId = "8AAfkhUKpU0sjeQrDtC6SrhRKNqA6hGb";
                //进行app身份校验
                activity.checkAppID(appId);
                //试戴指定sku的眼镜,点击可切换
                activity.wearGlasses(glassesInfo);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        int width = activity.getWindowManager().getDefaultDisplay().getWidth();
        dialog = new MyUniversalDialog(mContext);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setLayout(LayoutInflater.from(mContext).inflate(R.layout.dialog_share, null),
                MyUniversalDialog.DialogGravity.CENTER, -2, 5 * width / 6);
        RoundImageView roundImageView = (RoundImageView) dialog.findViewById(R.id.imageView);
        roundImageView.setRoundDegree(10);
        ImageView cancel = (ImageView) dialog.findViewById(R.id.cancel);
        TextView shareWechat = (TextView) dialog.findViewById(R.id.share_wechat);
        TextView shareQQ = (TextView) dialog.findViewById(R.id.share_qq);
        TextView shareWechatfriend = (TextView) dialog.findViewById(R.id.share_wechatfriend);
        shareWechat.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                share_WxFriend();
            }
        });
        shareQQ.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                share_QQFriend();
            }
        });
        shareWechatfriend.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                share_CircleFriend();
            }
        });
        cancel.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
    }


    /**
     * 分享到微信好友
     */
    private void share_WxFriend() {
        if (!Util.isWeixinAvilible(mContext)) {
            ToastHelper.getInstance()._toast("请先安装微信");
            return;
        }

        platform_wxFriend = ShareSDK.getPlatform(Wechat.NAME);
        Platform.ShareParams sp = new Platform.ShareParams();
        sp.setShareType(Platform.SHARE_WEBPAGE);// 一定要设置分享属性
        sp.setTitle("123");
        sp.setText("646516516");
        sp.setImageUrl("https://wx3.sinaimg.cn/mw690/71021e17gy1fi571ea8ykj20c80bawep.jpg");
        sp.setUrl("http://blog.csdn.net/awnuxcvbn/article/details/50737054");
        platform_wxFriend.setPlatformActionListener(TryToGlassesView.this); // 设置分享事件回调
        // 执行图文分享
        platform_wxFriend.share(sp);
    }


    /**
     * 分享到QQ好友
     */
    private void share_QQFriend() {
        if (!Util.isQQClientAvailable(mContext)) {
            ToastHelper.getInstance()._toast("请先安装QQ");
            return;
        }
        platform_qqFriend = ShareSDK.getPlatform(QQ.NAME);
        cn.sharesdk.tencent.qq.QQ.ShareParams sp = new cn.sharesdk.tencent.qq.QQ.ShareParams();
        sp.setShareType(Platform.SHARE_WEBPAGE);// 一定要设置分享属性
        sp.setTitle("123");
        sp.setTitleUrl("");
        sp.setText("646516516");
        sp.setImageUrl("");
        sp.setImagePath(null);
        platform_qqFriend.setPlatformActionListener(TryToGlassesView.this); // 设置分享事件回调
        // 执行图文分享
        platform_qqFriend.share(sp);
    }

    /**
     * 分享到朋友圈
     */
    private void share_CircleFriend() {
        if (!Util.isWeixinAvilible(mContext)) {
            ToastHelper.getInstance()._toast("请先安装微信");
            return;
        }
        platform_circle = ShareSDK.getPlatform(WechatMoments.NAME);
        cn.sharesdk.wechat.moments.WechatMoments.ShareParams sp = new cn.sharesdk.wechat.moments.WechatMoments.ShareParams();
        sp.setShareType(Platform.SHARE_WEBPAGE);// 一定要设置分享属性
        sp.setTitle("123");
        sp.setText("646516516");
        sp.setImageUrl("http://sta.273.com.cn/app/mbs/img/mobile_default.png");
        sp.setUrl("http://blog.csdn.net/awnuxcvbn/article/details/50737054");
        platform_circle.setPlatformActionListener(this); // 设置分享事件回调
        // 执行图文分享
        platform_circle.share(sp);
    }

    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        adapter.setSelectItem(position);  //当滑动时，事件响应，调用适配器中的这个方法。

    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {//抽象方法，必须实现
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_share:
                dialog.show();
                break;
        }
    }


    @Override
    public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
        Log.e(this.getClass().getSimpleName(), "onComplete");
    }

    @Override
    public void onError(Platform platform, int i, Throwable throwable) {
        Log.e(this.getClass().getSimpleName(), "onError" + throwable);
    }

    @Override
    public void onCancel(Platform platform, int i) {
        Log.e(this.getClass().getSimpleName(), "onCancel");
    }
}
